//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace OpenLS.Spreadsheet.UI
{
    public class BooleanToVisibilityConverter : IValueConverter
    {
        private Visibility falseVisibility = Visibility.Collapsed;

        public Visibility VisibilityIfFalse
        {
            get { return falseVisibility; }
            set { falseVisibility = value; }
        }


        public static BooleanToVisibilityConverter Instance
        {
            get { return new BooleanToVisibilityConverter(); }
        }

        public static BooleanToVisibilityConverter InstanceHidden
        {
            get { return new BooleanToVisibilityConverter(); }
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool b = false;
            if (value is bool)
            {
                b = (bool) value;
            }
            else if (value is Nullable<bool>)
            {
                Nullable<bool> nullable1 = (Nullable<bool>) value;
                b = nullable1.HasValue ? nullable1.Value : false;
            }
            else if (value != null)
                b = true;
            return (b ? Visibility.Visible : falseVisibility);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Visibility)
            {
                return (((Visibility) value) == Visibility.Visible);
            }
            return false;
        }

        #endregion
    }
}